package fi.honkalampisaatio.sound;

import java.io.File;

import javax.sound.sampled.Clip;

import fi.honkalampisaatio.game.utilities.MediaLoader;

import jaco.mp3.player.MP3Player;

public class SoundPlayer {
	MP3Player mp3Player;
	Clip soundclip;
	
	public boolean setSoundFile(String filename)
	{
		boolean retval = false;
		if(!new File(filename).isFile())
		{
			return false;
		}
		if(filename.endsWith("mp3"))
		{
			try {
				mp3Player = new MP3Player(new File(filename));
				mp3Player.setRepeat(false);
				retval = true;
				System.out.println("MP3Player created " + filename);
			} catch (Exception e) {
				System.err.println("Exception in " + filename);
				e.printStackTrace();
			}
		}
		else if(filename.endsWith("wav"))
		{
			soundclip = MediaLoader.createSoundClip(filename);
			if(soundclip!=null)
				retval = true;
		}
		return retval;
	}
	
	public boolean isLoaded()
	{
		return (mp3Player!=null || soundclip!=null);
	}
	
	public void play()
	{
		if(mp3Player!=null)
		{
			mp3Player.play();
		}
		else if(soundclip!=null)
		{
			soundclip.setMicrosecondPosition(0);
			soundclip.start();
		}
	}

	
	public void stop()
	{
		if(mp3Player!=null)
		{
			mp3Player.stop();
		}
		else if(soundclip!=null)
		{
			soundclip.stop();
		}
	}
}
